<?php
Class npcModel extends AppModel{
    private $pageSize = 20;
    public function getHome()
    {
        $sql  = "SELECT id,place,url FROM ".$this->table("npc_home")." ORDER BY place asc ";
        $result =  $this->getAll($sql);
        foreach ($result as $k=>$v)
        {
            $result[$k]['cdn'] = "http://g4cdn.airefp.com/ws/Static/npc/".$v['place'].".png";
        }
        return $result;
    }
    
	public function listNpc($page)
	{
		$listPage=array('data'=>'','pageLink'=>'');
		$where = " 1=1  order by create_time desc ";
		$result=$this->get_page(" * ",$this->table('npc'),$where,$page,$this->pageSize);
		foreach ($result as $k=>$v)
		{
		    $result[$k]['cdn'] = "http://g4.airefp.com/ws/index.php?g=mobile&m=npc&a=index&id=".$v['id'];
		}
		$listPage['pagelink']=$this->getpagelink();
		$listPage['data']=$result;
		$sql="SELECT count(*) FROM ".$this->table('npc');
		$count=$this->getOne($sql);
		$listPage['count']=$count;
		return $listPage;
	}
	
	public function get($id,$table)
	{
		$sql="SELECT * FROM ".$this->table($table)." WHERE id = '".$id."'";
		$result=$this->getRow($sql);
		if($result)
		{
			return $result;
		}
		else
		{
			return $result=array();
		}
	}
	
	public function getDefaultNid()
	{
		$nid = isset($_GET['nid'])?intval($_GET['nid']):"";
		if($nid)
		{
			return $nid;
		}else{
			$sql="SELECT id FROM ".$this->table("npc")." WHERE 1 = 1 order by create_time desc LIMIT 1 ";
			$nid=$this->getOne($sql);
			return $nid;
		}
	}
	
	public function listComment($page = 1,$total = array())
	{
		$where = " " ;
		$logic = "AND";
		foreach($total as $k=>$v)
		{
			if($k=="startTime"&&$v!=""){
				$where.=" p1.create_time>=".$v." ".$logic;
			}elseif ($k=="endTime"&&$v!=""){
				$where.=" p1.create_time<=".$v." ".$logic;
			}elseif($k=="comment"&&$v!=""){
				$where.=" p1.comment like '%".$v."%' ".$logic;
			}elseif($k=="nickname"&&$v!=""){
				$where.=" p2.nickname like '%".$v."%' ".$logic;
			}elseif($k=="nid"){
				$where.=" p1.nid = '".$v."' ".$logic;
			}elseif($k=="status"){
			    if($v!="all"){
			        if($v==2)
			        {
        				$where.=" p1.status = 0 ".$logic;
			        }else{
        				$where.=" p1.status = '".$v."' ".$logic;
			        }
			      
			    }
			}
		}
		$where .= " 1=1  order by  p1.create_time desc ";
		$listPage=array('data'=>'','pageLink'=>'');
		$result=$this->get_page(" p1.id,p1.comment,p1.status,p1.create_time,p2.openid,p2.nickname ",$this->table('npc_comment')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid = p2.wxid ",$where,$page,$this->pageSize);
		$listPage['pagelink']=$this->getpagelink();
		$listPage['data']=$result;
		$sql="SELECT count(*) FROM ".$this->table('npc_comment')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid = p2.wxid WHERE ".$where;
		$count=$this->getOne($sql);
		$listPage['count']=$count;
		return $listPage;
	}
	public function listCommentOldest($page = 1)
	{
		$where = " 1=1 AND p1.status in (0,2) order by  p1.create_time desc ";
		$listPage=array('data'=>'','pageLink'=>'');
		$result=$this->get_page(" p1.id,p1.comment,p1.status,p1.create_time,p2.nickname ",$this->table('npc_comment')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid = p2.wxid ",$where,$page,$this->pageSize);
		$listPage['pagelink']=$this->getpagelink();
		$listPage['data']=$result;
// 		$sql="SELECT count(*) FROM ".$this->table('track_comment')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid = p2.wxid WHERE ".$where;
// 		$count=$this->getOne($sql);
// 		$listPage['count']=$count;
		return $listPage;
	}
	
	
	public function addDigest($id){
	    $result=array('error'=>0,'content'=>'精选修改成功');
	    $time = time();
		$sql ="SELECT status FROM ".$this->table('npc_comment')." WHERE id=$id";
		$status = $this->getOne($sql);
        if($status==0){
            $sql ="UPDATE ".$this->table('npc_comment')." SET status=1 WHERE id=$id";
            $content = "已移入精选";
        }else{
            $sql ="UPDATE ".$this->table('npc_comment')." SET status=0 WHERE id=$id";
            $content = "已移出精选";
        }
		$res=$this->query($sql);
		if(!$res){
			$result['error']=1;
			$result['content']='精选修改精失败';
		}else{
		    $result['content'] = $content;
		}
		return $result;
		}
}